# Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import os

import pytest

from sagemaker.tensorflow import TensorFlow
from sagemaker.tuner import HyperparameterTuner, IntegerParameter

from ..... import invoke_sm_helper_function
from ...integration.utils import processor, py_version, unique_name_from_base  # noqa: F401


@pytest.mark.integration("hpo")
@pytest.mark.model("N/A")
@pytest.mark.team("frameworks")
def test_model_dir_with_training_job_name(
    ecr_image, sagemaker_regions, instance_type, framework_version
):
    invoke_sm_helper_function(
        ecr_image,
        sagemaker_regions,
        _test_model_dir_with_training_job_name_function,
        instance_type,
        framework_version,
    )


def _test_model_dir_with_training_job_name_function(
    ecr_image, sagemaker_session, instance_type, framework_version
):
    resource_path = os.path.join(os.path.dirname(__file__), "../..", "resources")
    script = os.path.join(resource_path, "tuning_model_dir", "entry.py")

    estimator = TensorFlow(
        entry_point=script,
        role="SageMakerRole",
        instance_type=instance_type,
        instance_count=1,
        image_uri=ecr_image,
        framework_version=framework_version,
        py_version="py3",
        sagemaker_session=sagemaker_session,
    )

    tuner = HyperparameterTuner(
        estimator=estimator,
        objective_metric_name="accuracy",
        hyperparameter_ranges={"arbitrary_value": IntegerParameter(0, 1)},
        metric_definitions=[{"Name": "accuracy", "Regex": "accuracy=([01])"}],
        max_jobs=1,
        max_parallel_jobs=1,
    )

    # User script has logic to check for the correct model_dir
    tuner.fit(job_name=unique_name_from_base("test-tf-model-dir", max_length=32))
    tuner.wait()
